package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Peachblossom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service(version = "1.0.0")
public class PeachblossomApiImpl implements PeachblossomApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    //保存桃花传音
    public Integer savePeachblossom(Peachblossom peachblossom) {
       //查询是否已发送桃花传音
        //今天
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        //查看当天该id发送多少条消息
        Query query = Query.query(Criteria.where("ID").is(peachblossom.getId()).and("starttime").is(dateString));
        Integer count = Math.toIntExact(mongoTemplate.count(query, Peachblossom.class));
        //剩余次数
        peachblossom.setRemainingTimes(10-count);
        if ((9-count)<0){
            //次数不够不保存
            return null;
        }
        mongoTemplate.save(peachblossom,"Peachblossom-user");
        return peachblossom.getId();
    }

    @Override
    //获取桃花传音
    public Peachblossom getPeachblossom(Integer id) {
        //不获得该id的桃花传音，
        //今天
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        //查看当天该id接受多少条消息
        System.out.println(id);
        System.out.println(dateString);
        Query query = Query.query(Criteria.where("getID").is(String.valueOf(id)).and("gettime").is(dateString));
        List<Peachblossom> peachblossoms1 = mongoTemplate.find(query, Peachblossom.class);
        int size = peachblossoms1.size();
        System.out.println(size+"---");
        if ((10-size)==0){
            //次数不够不获取
            return null;
        }
        //排除该id发的桃花传音，且使用id为空
        Query query1 = Query.query(Criteria.where("ID").ne(id).and("getID").is(null));
        List<Peachblossom> peachblossoms = mongoTemplate.find(query1, Peachblossom.class);
        if (peachblossoms.size()==0){
            return null;
        }
        Random random = new Random();
        int i = random.nextInt(peachblossoms.size());
        //随机获得一个桃花传音
        Peachblossom peachblossom = peachblossoms.get(i);
        //封装接受时间
        peachblossom.setGettime(dateString);
        //封装接受人
        peachblossom.setGetID(id.toString());
        //封装接受剩余次数
        peachblossom.setGETremainingTimes(Math.toIntExact(9 - size));
        //提交回mongodb
        mongoTemplate.update(peachblossom.getClass());
        Query query2 = Query.query(Criteria.where("ID").is(peachblossom.getId()).and("soundUrl").is(peachblossom.getSoundUrl()));
        Update update = new Update().set("GETremainingTimes",9-size);
        String ID = id.toString();
        update.push("getID",ID);
        update.push("gettime",dateString);
        mongoTemplate.updateFirst(query2,Update.update("getID",ID).set("gettime",dateString).set("GETremainingTimes",10-size),"Peachblossom-user");
        return peachblossom;
    }
}
